package com.eh.singleton;

/**
 * 巧克力锅炉
 * <p>
 * Created by David on 2016/4/29.
 */
public class ChocolateBoiler {
    private volatile static ChocolateBoiler chocolateBoiler;

    private boolean empty;
    private boolean boiled;

    private ChocolateBoiler() {
        empty = true;
        boiled = false;
    }

    public static ChocolateBoiler getInstance() {
        if (chocolateBoiler == null) {
            synchronized (ChocolateBoiler.class) {
                if (chocolateBoiler == null) {
                    chocolateBoiler = new ChocolateBoiler();
                }
            }
        }
        return chocolateBoiler;
    }

    public void fill() {
        if (isEmpty()) {
            empty = false;
            boiled = false;
            System.out.printf("将锅炉内填满巧克力和牛奶的混合物, 炉内状态[%s, %s]%n", isEmpty() ? "空" : "填满",
                    isBoiled() ? "煮沸" : "冷却");
        }
    }

    public void boil() {
        if (!isEmpty() && !isBoiled()) {
            System.out.println("将炉内物煮沸");
            boiled = true;
        }
    }

    public void drain() {
        if (!isEmpty() && isBoiled()) {
            System.out.println("排出煮沸的巧克力和牛奶");
            empty = true;
        }
    }

    public boolean isEmpty() {
        return empty;
    }

    public boolean isBoiled() {
        return boiled;
    }
}
